/**
 *@description:TODO
 * @author: cai
 *@time:2016年11月7日下午5:04:57
 */
package com.pos.modules.hang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.pos.R;
import com.pos.modules.cashier.bean.GoodsBean;
import com.pos.modules.hang.bean.HangGoodsBean;
import com.pos.toolbox.FinancialUtils;
import com.pos.toolbox.Tools;
import com.pos.toolbox.system.DeviceAttribute;
import com.pos.widgets.swipe.CustomSwipeListView;
import com.pos.widgets.swipe.SwipeItemView;
import com.pos.widgets.swipe.SwipeItemView.OnSlideListener;

import java.util.List;

/**
 * @description:新增基础合同list adapter
 * @author cai
 * @time:2016年11月7日下午5:04:57
 */
public class HangListAdapter extends ArrayAdapter<HangGoodsBean> {
	private Context mContext;
	private SwipeItemView mLastSlideViewWithStatusOn;
	private List<HangGoodsBean> hangList;
	private HangAction hangAction;

	public HangListAdapter(Context context, List<HangGoodsBean> hangList, HangAction hangAction) {
		super(context, 0, hangList);
		mContext = context;
		this.hangList = hangList;
		this.hangAction = hangAction;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		SwipeItemView slideView = (SwipeItemView) convertView;
		if (slideView == null) {

			View itemView = LayoutInflater.from(getContext()).inflate(R.layout.hang_goods_item_layout, null);

			itemView.setLayoutParams(
					new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, DeviceAttribute.dip2px(mContext, 120)));
			slideView = new SwipeItemView(mContext);
			slideView.setContentView(itemView);

			holder = new ViewHolder(slideView);
			slideView.setOnSlideListener(new OnSlideListener() {

				@Override
				public void onSlide(View view, int status) {
					// TODO Auto-generated method stub
					if (mLastSlideViewWithStatusOn != null && mLastSlideViewWithStatusOn != view) {
						mLastSlideViewWithStatusOn.shrink();
					}

					if (status == SLIDE_STATUS_ON) {
						mLastSlideViewWithStatusOn = (SwipeItemView) view;
					}
				}
			});
			slideView.setTag(holder);
		} else {
			holder = (ViewHolder) slideView.getTag();
		}
		HangGoodsBean item = getItem(position);
		if (CustomSwipeListView.mFocusedItemView != null) {
			CustomSwipeListView.mFocusedItemView.shrink();
		}
		holder.text_name.setText("操作员：" + item.getName());
		holder.text_time.setText("操作时间：" + Tools.stampToDate(item.getDate() + ""));
		List<GoodsBean> goodsList = item.getGoodsList();
		String detail = "";
		long totalFen = 0;
		for (GoodsBean goodsBean : goodsList) {
			totalFen = totalFen + goodsBean.getPrice()*goodsBean.getNum();
			detail = detail + "," + goodsBean.getCode() + "|" + goodsBean.getNum();
		}
		detail = "商品详情：" + detail.substring(1, detail.length());
		holder.text_detail.setText(detail);
		holder.text_total.setText("¥" + FinancialUtils.fen2yuan(totalFen));
		holder.text_get.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				hangAction.onGet(position);
			}
		});

		holder.deleteHolder.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				hangAction.onDelete(position);
			}
		});
		return slideView;
	}

	static class ViewHolder {
		public ImageView image_type;
		public TextView text_total, text_detail, text_name, text_time, text_get;
		public ViewGroup deleteHolder;

		ViewHolder(View view) {
			text_name = (TextView) view.findViewById(R.id.text_name);
			text_total = (TextView) view.findViewById(R.id.text_total);
			text_detail = (TextView) view.findViewById(R.id.text_detail);
			text_time = (TextView) view.findViewById(R.id.text_time);
			text_get = (TextView) view.findViewById(R.id.text_get);
			deleteHolder = (ViewGroup) view.findViewById(R.id.holder);

		}
	}

	public interface HangAction {

		public void onDelete(int position);

		public void onGet(int position);

	}

}
